package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.bean.BitmapBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.SettingData;
import com.store.app.utils.UtilsBitmap;
import com.yalantis.ucrop.CropUtils;
import com.yalantis.ucrop.UCrop;

import org.json.JSONObject;

import java.io.File;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/31.
 * 添加设备页面
 */

public class AddDeviceActivity extends BaseActivity implements IOnHttpListener, View.OnClickListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ImageView iv;
    private EditText et_device_name;
    private EditText et_location;
    private Button bt_submit;
    private static final int FLAG_CHOOSE_IMG = 5;
    private static final int FLAG_CHOOSE_PHONE = 6;
    private static String localTempImageFileName = "";
    private Dialog dialog;
    private BitmapBean bitmapBean;
    private NetRequest netRequest;
    private String deviceName;
    private String location;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_device);
        netRequest = new NetRequest(this);
        initView();
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        //上传图片成功
        if(requestId == 1){
            try {
                JSONObject object = new JSONObject(result);
                String doc_id = object.getString("doc_id");

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        iv = (ImageView) findViewById(R.id.iv);
        et_device_name = (EditText) findViewById(R.id.et_device_name);
        et_location = (EditText) findViewById(R.id.et_location);
        bt_submit = (Button) findViewById(R.id.bt_submit);
        tvTitle.setText("添加设备");
        bt_submit.setOnClickListener(this);
        public_ll_return.setOnClickListener(this);
        iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.bt_submit:
                if(bitmapBean==null){
                    showToast("请上传设备图片");
                    return;
                }
                deviceName = et_device_name.getText().toString().trim();
                if (TextUtils.isEmpty(deviceName)) {
                    showToast("请输入设备名称");
                    return;
                }
                location = et_location.getText().toString().trim();
                if (TextUtils.isEmpty(location)) {
                    showToast("请输入设备所在位置");
                    return;
                }

                netRequest.requestUploadPic(1,bitmapBean.getIsBm());
                //showProgressDialog("","正在提交中...");
                break;
            case R.id.iv:
                getPic();
                break;
        }
    }

    public void getPic() {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new AlertDialog.Builder(this)
                .setTitle("请选择图片来源")
                .setItems(new String[]{"相册", "相机"},
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (which == 0) {
                                    Intent intent = new Intent(
                                            Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                    startActivityForResult(intent,
                                            FLAG_CHOOSE_IMG);
                                } else if (which == 1) {
                                    String status = Environment
                                            .getExternalStorageState();
                                    if (status
                                            .equals(Environment.MEDIA_MOUNTED)) {
                                        try {
                                            localTempImageFileName = "";
                                            localTempImageFileName = String
                                                    .valueOf((new Date())
                                                            .getTime())
                                                    + ".png";
                                            File filePath = SettingData.FILE_PIC_SCREENSHOT;
                                            if (!filePath.exists()) {
                                                filePath.mkdirs();
                                            }
                                            Intent intent = new Intent(
                                                    MediaStore.ACTION_IMAGE_CAPTURE);
                                            File f = new File(filePath,
                                                    localTempImageFileName);
                                            // localTempImgDir和localTempImageFileName是自己定义的名字
                                            Uri u = Uri.fromFile(f);
                                            intent.putExtra(
                                                    MediaStore.Images.Media.ORIENTATION,
                                                    0);
                                            intent.putExtra(
                                                    MediaStore.EXTRA_OUTPUT,
                                                    u);
                                            startActivityForResult(
                                                    intent,
                                                    FLAG_CHOOSE_PHONE);
                                        } catch (ActivityNotFoundException e) {
                                            //
                                        }
                                    }
                                }
                            }
                        }).show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == FLAG_CHOOSE_IMG && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {
                    CropUtils.startCropActivity(uri, AddDeviceActivity.this, 5, 5);
                }
            }
        } else if (requestCode == FLAG_CHOOSE_PHONE && resultCode == RESULT_OK) {
            Log.d("lxp", "=========");
            File f = new File(SettingData.FILE_PIC_SCREENSHOT, localTempImageFileName);
            Uri uri = Uri.fromFile(f);
            if (uri != null) {
                UtilsBitmap.getCompressUri(uri);
                CropUtils.startCropActivity(uri, AddDeviceActivity.this, 5, 5);
            }
        }


        //拿到裁剪后的图片
        if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) {
            if (data != null) {
                final Uri resultUri = UCrop.getOutput(data);
                bitmapBean = UtilsBitmap.addFilePath3(resultUri.getPath(), AddDeviceActivity.this);
                iv.setImageBitmap(bitmapBean.getBitmap());
            }
        } else if (resultCode == UCrop.RESULT_ERROR) {
            Toast.makeText(this, "图片剪切失败！", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
